package com.baozun.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baozun.mapper.TFindMailMapper;
import com.baozun.model.TFindMail;
import com.baozun.model.TFindMailExample;
import com.baozun.service.FindMailService;
import com.baozun.util.PageData;


@Service("findMailService")
@Transactional
public class FindMailServiceImpl implements FindMailService {
    @Autowired
    private TFindMailMapper findMailMapper;

    @Override
    public void add(TFindMail bean) {
        if (StringUtils.isEmpty(bean.getStFindMailId())) {
            bean.setStFindMailId(UUID.randomUUID().toString());
        }
        bean.setDtCreateTime(new Date());
        findMailMapper.insert(bean);
    }

    @Override
    public TFindMail get(String id) {
        return findMailMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(TFindMail bean) {
        bean.setDtUpdateTime(new Date());
        findMailMapper.updateByPrimaryKeyWithBLOBs(bean);
    }

    @Override
    public void delete(String stFindMailId) {
        // TODO Auto-generated method stub
        findMailMapper.deleteByPrimaryKey(stFindMailId);
    }

    @Override
    public PageData<TFindMail> getPageListByExample(TFindMailExample record, int pageSize, int currentPage) {
        record.setSize(pageSize);
        record.setStart(pageSize * (currentPage - 1));
        PageData<TFindMail> infoList = new PageData<TFindMail>();
        List<TFindMail> appendixList = findMailMapper.selectByExample(record);
        int count = findMailMapper.countByExample(record);
        infoList.addAll(appendixList);
        infoList.setPage(currentPage);
        infoList.setPages((count + pageSize - 1) / pageSize);
        infoList.setPageSize(pageSize);
        infoList.setTotalRecords(count);
        return infoList;
    }

    @Override
    public List<TFindMail> getList(TFindMailExample record) {
        // TODO Auto-generated method stub
        return findMailMapper.selectByExample(record);
    }

}
